// pages/my/vip/vip_ coupon/index.js
const app=getApp();
const couponList=require("../../../controllers/ControllerVip").controller.couponList;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalStype:'',
    allListMy:[],
    allListOther:[],
    noUseListMy:[],
    noUseListOther:[],
    historyListMy:[],
    historyListOther:[],
    showListMy:[],
    showListOther:[],
    src1:"../../../../assets/my/img/quan_bg.png",
    src2:"../../../../assets/my/img/quan_unsel.png",
    src3:"../../../../assets/my/img/quan_gq.png",
    type:1,
    shopId:""
  },
  onLoad: function (options) {
    this.setData({globalStype:app.globalData.globalStype})
    if(options.shopId){
      this.setData({
        shopId:options.shopId
      })
    }else{
      this.setData({
        shopId:app.globalData.shopId
      })
    }
  },

  onShow: function () {
    this.getCouponList()
  },

  selectTab(e){
    var num=e.currentTarget.dataset['index'];
    this.setData({
      type:num,
      showListMy:[],
      showListOther:[],
    })
    this.getCouponList()
    if(num==1){
      this.setData({
        selectAll:true,
        selectUnuse:false,
        selectHistory:false,
        showListMy:this.data.allListMy,
        showListOther:this.data.allListOther
      })
    }else if(num==2){
      this.setData({
        selectAll:false,
        selectUnuse:true,
        selectHistory:false,
        showListMy:this.data.noUseListMy,
        showListOther:this.data.noUseListOther
      })
    }else{
      this.setData({
        selectAll:false,
        selectUnuse:false,
        selectHistory:true,
        showListMy:this.data.historyListMy,
        showListOther:this.data.historyListOther
      })
    }
  },

  // 获取优惠券列表
  getCouponList(){
    
    let data={shopId:this.data.shopId}
    if(this.data.type==2){
      data.state=0
    }
    if(this.data.type==3){
      data.state=1
    }
    couponList(data).then(res=>{
      let my=res.data.my;
      let other=res.data.other;

      for(var i=0;i<my.length;i++){
        if(my[i].timeState!=0){
          if(my[i].effectiveStartTime){
            my[i].effectiveStartTime=my[i].effectiveStartTime.split(" ")[0];
          }
          if(my[i].effectiveEndTime){
            my[i].effectiveEndTime=my[i].effectiveEndTime.split(" ")[0];
          }
          
        }
      }
      for(var i=0;i<other.length;i++){
        if(other[i].timeState!=0){
         if(other[i].effectiveStartTime){
          other[i].effectiveStartTime=other[i].effectiveStartTime.split(" ")[0];
         }
         if(other[i].effectiveEndTime){
          other[i].effectiveEndTime=other[i].effectiveEndTime.split(" ")[0];
         }
        }
      }
      this.setData({
        showListMy:my,
        showListOther:other
      })
    })
  },

  //跳转优惠券详情页面
  jumpToDetail(e){
    let cstate=e.currentTarget.dataset.cstate;
    let id=e.currentTarget.dataset.id;
    let state=e.currentTarget.dataset.state
    let pushid=e.currentTarget.dataset.pushid
    let couponaccountid=e.currentTarget.dataset.couponaccountid
    let url='/pages/Mall/Coupondetail?fromUrl=1&id='+id+"&type="+state+"&couponPushId="+pushid+'&isuserdetail=1'+'&couponAccountId='+couponaccountid
    if(cstate===0){
      wx.navigateTo({
        url: url,
      })
    }else{
      wx.showToast({
        icon:"none",
        title: '会员卡已过期/已使用',
      })
    }
  }
})